﻿
function getTargetName(defTarget){
	var bases = document.getElementsByTagName("base");
    if (bases && bases.length && bases[0].target) {
        return bases[0].target;
	}else{
		return defTarget?defTarget:"_self";
	}
}

function getFrame(name, aWindow){
	var win = aWindow?aWindow:window;
	if(win.frames) {
		if(win.frames[name]){
			return win.frames[name];
		}else{
			if(win.parent==win.self)return null;
			return (win.parent)?getFrame(name, win.parent):win;
		}
	}else{
		return (win.parent)?getFrame(name, win.parent):win;
	}
}

function getParentNodeByTagName(selfNode,parentName,n){
 	if(!selfNode)return null;
	var i=0;
	var tNode = selfNode;
	var tName = parentName.toUpperCase();
 	var p=parseInt(n);
	if(isNaN(p))p=0;
	while(true){
		if(tNode.tagName=="BODY"){
	  		break;
	  	}else if(tNode.tagName!=tName){
	     	tNode = tNode.parentNode;
	  	}else{
	  		if(i<p)i++;
	  		else break;
	  	}
	}
	if(tNode.tagName!=tName||i<p)obj=null;
	return tNode;
}

function getElementsByAttribute(el,an,av){
	var elems = el.getElementsByTagName("*");
	var elemList = [];
	for (var i=0,j; j=elems[i]; i++){
		var att = j[an] || j.getAttribute(an);
		if (att==av){
			elemList.push(j);
		}
	}
	return elemList;
}


/* get dom by target, 
 * if target is null then gen new, 
 * if target.canHaveChildren return target 
 * will create Dom object, then rename to Dom.getNode(target);
 * */
 function getDomNode(target){
	var theContainer;
	if(!target){
		theContainer = genDomNode("div","_divContainer_");
	}else if(typeof(target)=="string"){
		theContainer = document.getElementById(target);
	}else if(target.canHaveChildren){
		theContainer = target;
	}else{
		theContainer = genDomNode("div","_divContainer_");
	}
	return theContainer;
 }
 
/* @depricated
 * will use getDomNode(target) 
 */ 
function getContainer(target){
	 return getDomNode(target);
}

function genDomNode(tagName,idPrefix){
		var nodeName = tagName?tagName.toUpperCase():"DIV";
		var randomId=(idPrefix?idPrefix:nodeName+"_random_")+getRandom(8);
		var strHtml="<"+nodeName+" id=\""+randomId+"\"></"+nodeName+">";
		document.write(strHtml);
		return document.getElementById(randomId);
}

//if ancestorNode is childNode's ancestor in dom tree, return true;
function isAncestorOf(childNode,ancestorNode){
	if(!ancestorNode)return false;
	var pNode = childNode;
	while(true){
		if(!pNode){
			return false;
		}else if(pNode==ancestorNode){
			return true;
		}else{
			pNode = pNode.parentElement;
		}
	}
}

function removeTableRowNode(aNode){
	var aRow = getParentNodeByTagName(aNode,"TR");
	if( aRow ) aRow.removeNode(true);
}


function $id(str){
	return document.getElementById(str);
}
 
function $name(str){
	return document.getElementsByName(str);
}
 
function $tag(str){
	return document.getElementsByTagName(str);
}
